package com.dhcc.cdc.busi.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.cdc.busi.dao.CdcOrdgroupEntryMapper;
import com.dhcc.cdc.busi.dao.CdcOrdgroupMapper;
import com.dhcc.cdc.busi.dao.CdcOrdmastMapper;
import com.dhcc.cdc.busi.dao.CdcUserMapper;
import com.dhcc.cdc.busi.entity.CdcOrdgroupEntity;
import com.dhcc.cdc.busi.entity.CdcOrdgroupEntryEntity;
import com.dhcc.cdc.busi.entity.CdcOrdmastEntity;
import com.dhcc.cdc.busi.entity.CdcUserEntity;
import com.dhcc.cdc.busi.service.ICdcOrdgroupService;
import com.dhcc.cdc.sys.dao.CdcDataMapper;
import com.dhcc.cdc.sys.entity.CdcDataEntity;
import com.dhcc.core.framework.base.service.impl.BaseServiceImpl;
import com.dhcc.core.framework.util.CommonUtil;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 医嘱套(组)表数据服务层接口
 * 
 * @ClassName: CdcOrdgroupServiceImpl
 * @Description: TODO
 * @author jumper
 * @date 2021-03-15 10:20:07
 */
@Service("cdcOrdgroupService")
public class CdcOrdgroupServiceImpl extends BaseServiceImpl<CdcOrdgroupMapper, CdcOrdgroupEntity>
		implements ICdcOrdgroupService {

	@Autowired
	private CdcUserMapper cdcUserMapper;

	@Autowired
	private CdcDataMapper cdcDataMapper;

	@Autowired
	private CdcOrdmastMapper cdcOrdmastMapper;
	@Autowired
	private CdcOrdgroupEntryMapper cdcOrdgroupEntryMapper;

	@Override
	public List<CdcOrdgroupEntity> page(Page<CdcOrdgroupEntity> page, Map<String, Object> map) {
		return this.baseMapper.page(page, map);
	}

	@Override
	public List<CdcOrdgroupEntity> list(Map<String, Object> map) {
		return this.baseMapper.list(map);
	}

	@Override
	public String handleXmlData(String dataStr) {

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		Document doc = null;
		try {
			doc = DocumentHelper.parseText(dataStr); // 将字符串转为XML
			Element rootElt = doc.getRootElement(); // 获取根节点
			Iterator<Element> iter = rootElt.elementIterator("ordergroup"); // 获取根节点下的子节点ordergroup
			// 遍历ordergroup节点
			while (iter.hasNext()) {
				Element ordgrpEle = iter.next();
				String code = ordgrpEle.elementTextTrim("code"); // 代码
				String description = ordgrpEle.elementTextTrim("description"); // 描述
				String updatetimestr = ordgrpEle.elementTextTrim("updatetime"); // 更新时间
				String updateuserid = ordgrpEle.elementTextTrim("updateuserid"); // 更新人
				String opercode = ordgrpEle.elementTextTrim("opercode"); // 操作码
				Element eleOrdItems = ordgrpEle.element("orderitems"); // 获取根节点
				// 先保存医嘱套 ---再保存医嘱信息
				CdcOrdgroupEntity ordGrp = new CdcOrdgroupEntity();
				ordGrp.setCode(code);
				// 获取basedataId
				CdcDataEntity cdcDataEntityBase = new CdcDataEntity();
				cdcDataEntityBase.setCode("HisBase");
				cdcDataEntityBase.setIsActive(1);
				cdcDataEntityBase = cdcDataMapper.selectOne(cdcDataEntityBase);
				Long basedataId = cdcDataEntityBase.getId();
				//获取orddataId
    			CdcDataEntity cdcDataEntityOrd = new CdcDataEntity();
    			cdcDataEntityOrd.setCode("OrdData");
    			cdcDataEntityOrd.setIsActive(1);
    			cdcDataEntityOrd = cdcDataMapper.selectOne(cdcDataEntityOrd);
        		Long orddataId = cdcDataEntityOrd.getId();
				if (CommonUtil.isNotEmpty(updatetimestr)) {
					Date updatetime = sdf.parse(updatetimestr);
					ordGrp.setUpdateDate(updatetime);
				}
				// 获取更新人
				if (CommonUtil.isNotEmpty(updateuserid)) {
					CdcUserEntity cdcUserEntity = new CdcUserEntity();
					cdcUserEntity.setXcode(updateuserid);
					cdcUserEntity.setDataId(basedataId);
					cdcUserEntity.setIsActive(1);
					cdcUserEntity = cdcUserMapper.selectOne(cdcUserEntity);
					// 判断人员存在不存在
					Long updateuser = null;
					if (CommonUtil.isEmpty(cdcUserEntity)) {// 不存在 保存到用户表里面
						cdcUserEntity = new CdcUserEntity();
						cdcUserEntity.setXcode(updateuserid);
						cdcUserEntity.setIsActive(1);
						cdcUserEntity.setCode("newuser" + updateuserid);
						cdcUserEntity.setName("newuser" + updateuserid);
						cdcUserEntity.setDataId(basedataId);
						// 保存用户
						cdcUserMapper.insert(cdcUserEntity);
						updateuser = cdcUserEntity.getId();
					} else {
						updateuser = cdcUserEntity.getId();
					}
					ordGrp.setUpdateBy(updateuser);
				}
				if ("1".equals(opercode)) {// 保存/修改
					if (CommonUtil.isNotEmpty(description)) {
						ordGrp.setDescription(description);
					}
					ordGrp.setDataId(orddataId);
					ordGrp.setIsActive(1);
				} else {
					ordGrp.setIsActive(0);
				}
				// 先判断是不是有该对象
				Wrapper<CdcOrdgroupEntity> wrapper = new EntityWrapper<CdcOrdgroupEntity>();
				wrapper.eq("code", ordGrp.getCode());
				wrapper.eq("data_id", orddataId);
				int count = this.selectCount(wrapper);
				if (count > 0) {
					this.update(ordGrp, wrapper);
				} else {
					this.insert(ordGrp);
				}
				// 医嘱套医嘱结果
				Iterator<Element> iteritem = eleOrdItems.elementIterator("orderitem"); // 获取根节点下的子节点orderitem
				while (iteritem.hasNext()) {
					Element orditemEle = iteritem.next();
					String ordermastid = orditemEle.elementTextTrim("ordermastid"); // 医嘱项
					// 保存医嘱套医嘱结果
					CdcOrdgroupEntryEntity ordGrpEntry = new CdcOrdgroupEntryEntity();
					ordGrpEntry.setOrderGroupId(ordGrp.getId());
					// 根据医嘱条目id获取longid
					if (CommonUtil.isNotEmpty(ordermastid)) {
						// 如果没有对应ID就要插入
						CdcOrdmastEntity cdcOrdmastEntity = new CdcOrdmastEntity();
						cdcOrdmastEntity.setXcode(ordermastid);
						cdcOrdmastEntity.setDataId(orddataId);
						cdcOrdmastEntity.setIsActive(1);
						cdcOrdmastEntity = cdcOrdmastMapper.selectOne(cdcOrdmastEntity);
						if (CommonUtil.isNotEmpty(cdcOrdmastEntity)) {
							Long orderid = cdcOrdmastEntity.getId();
							ordGrpEntry.setOrderMastId(orderid);
						}
					}
					if (CommonUtil.isNotEmpty(updatetimestr)) {
						Date updatetime = sdf.parse(updatetimestr);
						ordGrpEntry.setUpdateDate(updatetime);
					}
					// 获取更新人
					if (CommonUtil.isNotEmpty(updateuserid)) {
						CdcUserEntity cdcUserEntity = new CdcUserEntity();
						cdcUserEntity.setXcode(updateuserid);
						cdcUserEntity.setDataId(basedataId);
						cdcUserEntity.setIsActive(1);
						cdcUserEntity = cdcUserMapper.selectOne(cdcUserEntity);
						// 判断人员存在不存在
						Long updateuser = null;
						if (CommonUtil.isEmpty(cdcUserEntity)) {// 不存在 保存到用户表里面
							cdcUserEntity = new CdcUserEntity();
							cdcUserEntity.setXcode(updateuserid);
							cdcUserEntity.setIsActive(1);
							cdcUserEntity.setCode("newuser" + updateuserid);
							cdcUserEntity.setName("newuser" + updateuserid);
							cdcUserEntity.setDataId(basedataId);
							// 保存用户
							cdcUserMapper.insert(cdcUserEntity);
							updateuser = cdcUserEntity.getId();
						} else {
							updateuser = cdcUserEntity.getId();
						}
						ordGrpEntry.setUpdateBy(updateuser);
					}
					if ("1".equals(opercode)) {// 保存/修改
						ordGrpEntry.setIsActive(1);
					} else {
						ordGrpEntry.setIsActive(0);
					}
					// 先判断是不是有该对象
					Wrapper<CdcOrdgroupEntryEntity> itemwrapper = new EntityWrapper<CdcOrdgroupEntryEntity>();
					itemwrapper.eq("order_mast_id", ordGrpEntry.getOrderMastId());
					itemwrapper.eq("order_group_id", ordGrpEntry.getOrderGroupId());
					int itemcount = this.selectCount(wrapper);
					if (itemcount > 0) {
						cdcOrdgroupEntryMapper.update(ordGrpEntry, itemwrapper);
					} else {
						cdcOrdgroupEntryMapper.insert(ordGrpEntry);
					}
				}
			}
		} catch (DocumentException e) {
			e.printStackTrace();
			return "<response><status>0</status><message>" + e.getMessage() + "</message></response>";
		} catch (Exception e) {
			e.printStackTrace();
			return "<response><status>0</status><message>" + e.getMessage() + "</message></response>";
		}
		return "<response><status>1</status><message>ok</message></response>";
	}
}